package com.ruixin.mvc.parser;

import com.ruixin.annotation.Parser;
import com.ruixin.base.BaseParser;
import com.ruixin.base.exception.BaseException;
import com.ruixin.base.util.BeanUtil;
import com.ruixin.mvc.annotation.RequestBody;
import com.ruixin.mvc.config.ApplicationContextWrapper;
import com.ruixin.mvc.config.MethodMapping;
import com.ruixin.mvc.config.WebMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Parser(annotation= RequestBody.class)
public class RequestBodyParser extends BaseParser<ApplicationContextWrapper> {

    public RequestBodyParser(ApplicationContextWrapper context, Class<? extends Annotation> type) {
        super(context, type);
    }

    @Override
    public void parseParamater(Object bean, Method method, Parameter parameter) throws BaseException {
        super.parseParamater(bean, method, parameter);
        WebMapping webMapping = this.context.getWebMapping(method.getDeclaringClass());
        RequestBody args = BeanUtil.getAnnotation(parameter, RequestBody.class);
        MethodMapping methodMapping = webMapping.getMethodMapping(method);
        methodMapping.setRequestBody(parameter);
    }
}
